﻿using RTW.OA.Areas.Personal.Servsers;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using RTW.OA.Models;
using Newtonsoft.Json;
using System.Web.Script.Serialization;
using RTW.OA.Areas.Personal.Models;

namespace RTW.OA.Areas.Personal.Controllers
{
    public class CalendarController : Controller
    {
        private CalendarServer calendarServer;

        public CalendarController()
        {
            calendarServer = new CalendarServer();
        }
        // GET: Personal/Calendar
        public ActionResult Index()
        {
            List<CalendarToFullCalendar> calendarToFullCalendarList = new List<CalendarToFullCalendar>();
            List<calendar> list = calendarServer.GetData();

            foreach (var item in list)
            {
                CalendarToFullCalendar calendarToFullCalendar = new CalendarToFullCalendar
                {
                    id = item.CAL_ID.ToString(),
                    title = item.CONTENT,
                    allday = item.ALLDAY == null ? false : Convert.ToBoolean(item.ALLDAY),
                    start = item.CAL_TIME == null ? "" : item.CAL_TIME.ToString(),
                    end = item.END_TIME == null ? "" : item.END_TIME.ToString(),
                    url = item.URL
                };
                calendarToFullCalendarList.Add(calendarToFullCalendar);
            }

           string  lists= Newtonsoft.Json.JsonConvert.SerializeObject(calendarToFullCalendarList);
            ViewData["calendarData"] = lists;
            return View();
        }

        // GET: Personal/Calendar/Details/5
        public ActionResult Details(int id)
        {
            return View();
        }

        // GET: Personal/Calendar/Create
        public ActionResult Select()
        {
            return View();
        }

        // POST: Personal/Calendar/Create
        [HttpPost]
        public ActionResult Create(FormCollection collection)
        {
            try
            {
                // TODO: Add insert logic here
                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }

        // GET: Personal/Calendar/Edit/5
        public ActionResult Edit(int? id,string start,string end)
        {
            #region  事务类型 (1-工作事务,2-个人事务)
            ViewBag.CAL_TYPEData = calendarServer.GetCalType_TYPE();
            #endregion

            #region  优先级  (1-重要/紧急,2-重要/不紧急,3-不重要/紧急,4-不重要/不紧急)
            ViewBag.CAL_LEVELData = calendarServer.GetCalLevel_TYPE();
            #endregion

            #region  完成状态(0-未完成,1-已完成)
            ViewBag.OVER_STATUSData = calendarServer.GetCalOverstatus_TYPE();
            #endregion

            #region  全天状态(0-否,1-是)
            ViewBag.ALLDAYData = calendarServer.GetAllDay_TYPE();
            #endregion

            var model = new calendar();
            if (id !=null)
            {
                ViewBag.Title = "编辑日程";
                model = calendarServer.GetModelById(id.Value.ToString());
            }
            else
            {
                ViewBag.Title = "新建日程";
                if (!string.IsNullOrWhiteSpace(start))
                {
                    model.CAL_TIME = ConvertStringToDateTime(start);
                }
                if (!string.IsNullOrWhiteSpace(end))
                {
                    model.END_TIME = ConvertStringToDateTime(end);
                }
            }
            return View(model);
        }

        // POST: Personal/Calendar/Edit/5
        [HttpPost]
        public ActionResult Edit(int? CAL_ID, calendar model)
        {
            try
            {
                if (CAL_ID == null|| CAL_ID == 0)
                {
                    model.ADD_TIME = DateTime.Now;
                    calendarServer.Add(model);
                    ViewBag.msg = "新增日程安排成功！";
                    model = new calendar();
                }
                else
                {
                    model.ADD_TIME = DateTime.Now;
                    calendarServer.Update(model);
                    ViewBag.msg = "编辑日程安排成功！";
                    model = new calendar();
                }
                return RedirectToAction("Index");
            }
            catch
            {
               ViewBag.msg = "提交出现错误！";
             }
            return View(model);
        }

        // GET: Personal/Calendar/Delete/5
        public ActionResult Delete(int id)
        {
            calendarServer.Delete(id.ToString());
            return Content("");
        }

        // POST: Personal/Calendar/Delete/5
        [HttpPost]
        public ActionResult Delete(int id, FormCollection collection)
        {
            try
            {
                // TODO: Add delete logic here
                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }

        /// <summary>
        /// 13位时间戳转时间格式
        /// </summary>
        /// <param name="timeStamp"></param>
        /// <returns></returns>
        private DateTime ConvertStringToDateTime(string timeStamp)
        {
            DateTime dtStart = TimeZone.CurrentTimeZone.ToLocalTime(new DateTime(1970, 1, 1));
            long lTime = long.Parse(timeStamp + "0000");
            TimeSpan toNow = new TimeSpan(lTime);
            return dtStart.Add(toNow);
        }

        public string GetTableJsonResult(string CAL_TIME, string END_TIME, string CAL_TYPE, string CAL_LEVEL, string CONTENT, string OVER_STATUS)
        {
            DateTime? startTimeTemp = null;
            DateTime? endTimeTemp = null;
            if (!string.IsNullOrWhiteSpace(CAL_TIME))
            {
                startTimeTemp = Convert.ToDateTime(CAL_TIME);
            }
            if (!string.IsNullOrWhiteSpace(END_TIME))
            {
                endTimeTemp = Convert.ToDateTime(END_TIME);
            }
            var list = calendarServer.GetData(startTimeTemp, endTimeTemp, CAL_TYPE, CAL_LEVEL, CONTENT, OVER_STATUS);
            return JsonConvert.SerializeObject(list);
            }
    }
}
